package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.example.adapter.CategoryAdapter;
import com.example.bean.NewFragmentBean;
import com.example.config.UrlsConfig;
import com.example.fruit.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 2015/6/28.
 */
public class HotFragment extends Fragment {


        private HttpUtils httpUtils;
        private CategoryAdapter adapter;
        private List<NewFragmentBean> data=new ArrayList<NewFragmentBean>();
        private ListView listView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            httpUtils=new HttpUtils();
            adapter=new CategoryAdapter(getActivity(),data);



        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_allguoshit,container,false);
            listView= (ListView) view.findViewById(R.id.listId);
            initView();
            return view;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            loadData();
        }

        private void initView() {
            listView.setAdapter(adapter);


        }


        private void loadData() {
            RequestParams params=getRquestParams();
            httpUtils.send(HttpRequest.HttpMethod.POST, UrlsConfig.FIRST_PAGE, params, new RequestCallBack<String>() {


                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    //获得服务器返回的json数据
                    try {

                        JSONArray array = new JSONArray(responseInfo.result);
                        List<NewFragmentBean> news = JSON.parseArray(array.toString(), NewFragmentBean.class);
                        data.addAll(news);
                        adapter.notifyDataSetChanged();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }

                @Override
                public void onFailure(HttpException e, String s) {
                }
            });
        }



    private RequestParams getRquestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("page_size", "20");
        params.addBodyParameter("timestamp", "1435383090");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "");
        params.addBodyParameter("region_id", "143949");
        params.addBodyParameter("service", "product.category");
        params.addBodyParameter("device_id", "10b61d436aa7477f74006f903edb0f8a");
        params.addBodyParameter("channel", "qq");
        params.addBodyParameter("version", "2.1.1");
        params.addBodyParameter("sort", "0");
        params.addBodyParameter("sign", "51f54e6ce79f633719e3102529dde598");
        params.addBodyParameter("class_id", "40");
        params.addBodyParameter("curr_page", "0");
        return params;
    }
    }






